#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

typedef struct
{
    char name[4];
    int age;
} Student;

int main()
{
    int fd = open("stu.dat", O_CREAT | O_RDWR | O_TRUNC, 0666);
    if (fd == -1)
    {
        perror("open");
        return 1;
    }

    if (ftruncate(fd, 10 * sizeof(Student)) == -1)
    {
        perror("ftruncate");
        close(fd);
        return 1;
    }

    Student *p = mmap(NULL, 10 * sizeof(Student), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (p == MAP_FAILED)
    {
        perror("mmap");
        close(fd);
        return 1;
    }

    strcpy(p[0].name, "Tom");
    p[0].age = 20;

    if (munmap(p, 10 * sizeof(Student)) == -1)
    {
        perror("munmap");
    }

    close(fd);
    return 0;
}